package org.vectrics.recruiter.struts.tag;

/**
 * Title:        vectrics Server for Logging
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      vectrics Software Inc.  Service is Built In
 * @author Mike Moore
 * @version 1.0
 */
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.ecs.html.A;
import org.apache.log4j.Category;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.location.Location;
import org.vectrics.provider.CoreServiceLocator;


public class LocationListTag extends TagSupport {

	private static final long serialVersionUID = 5598040008469384537L;
	
	private String domainId = null;
    static private Category log = Category.getInstance(LocationListTag.class);
    public static String TABLE_FONT = "subFont";

	public int doStartTag() throws JspException  {
        log.debug("doRender() called");
		HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();        
        Table mainTable = new Table();
        Row mainRow = mainTable.addRow();
        Cell mainCell = mainRow.addCell();

        Domain domain = this.getDomain();
        
        if (domain == null) {
        	domain = ThreadProperties.getDomain();
        }
        
        Collection locationList = null; 
        try {
        	locationList = CoreServiceLocator.getLocationService().findAllLocations(domain);
        }
        catch (Exception ex) {
        	log.error("Exception getting location list");
        	throw new JspException(ex);
        }
        
        if ((locationList == null) || (locationList.size() == 0)) {
            mainCell.addElement(TextHelper.getFont("stdFont", "<br><i>No Locations</i>"));
        }
        else {
            
            BorderTable listTable = new BorderTable();
            listTable.setWidth(650);
            mainCell.addElement(listTable);
            Iterator iter = locationList.iterator();
            Row hdrRow = listTable.addRow();
            Cell hdrCell = hdrRow.addCell();
            hdrCell.addElement(TextHelper.getFont("smlTblHeader", "Code"));
            hdrCell = hdrRow.addCell();
            hdrCell.addElement(TextHelper.getFont("smlTblHeader", "Name"));
            hdrCell.setWidth(220);
            hdrCell = hdrRow.addCell();
            hdrCell.addElement(TextHelper.getFont("smlTblHeader", "Description"));
            hdrCell.setWidth(150);
			hdrCell = hdrRow.addCell();
			hdrCell.addElement(TextHelper.getFont("smlTblHeader", "Actions"));
			hdrCell.setWidth(150);
            while (iter.hasNext()) {
                Location location = (Location)iter.next();
                Row row = listTable.addRow();
                Cell cell = row.addCell();
                cell.addElement(TextHelper.getFont(TABLE_FONT, location.getId().toString()));
                cell = row.addCell();
                cell.addElement(TextHelper.getFont(TABLE_FONT, location.getName()));
                cell = row.addCell();
                cell.addElement(TextHelper.getFont(TABLE_FONT, location.getDescription()));
				cell = row.addCell();
				A removeLink = new A();
				removeLink.setHref(request.getContextPath() + "/admin/location/remove.do?id=" + location.getId());
				removeLink.setClass("subLink");
				removeLink.addElement("remove");
				cell.addElement(removeLink);
            }
        }
	
		JspWriter writer = pageContext.getOut();
		try {
			writer.print(mainTable.toString());
		}
		catch (IOException ex) {
			throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
		}
		return(EVAL_PAGE);
    }

	/**
	 * @return Returns the domainId.
	 */
	public String getDomainId() {
		return domainId;
	}

	/**
	 * @param domainId The domainId to set.
	 */
	public void setDomainId(String domainId) {
		this.domainId = domainId;
	}
    
	
	public Domain getDomain() {
		return(CoreServiceLocator.getSystemService().findDomain(new Long(this.getDomainId())));
	}
	
	
}
